﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project.Co_DataTransformationPrototype.UI
{
    public partial class EventManagerForm : Form
    {
        private ElementEventInfo selectedEventInfo;
        private EventContext selectedEventContext;
        private IDictionary<Guid, ElementEventInfo> eventCollection;
        
        public EventManagerForm(IDictionary<Guid, ElementEventInfo> eventCollection)
        {
            this.eventCollection = eventCollection;
            InitializeComponent();
        }

        private void EventManagerForm_Load(object sender, EventArgs e)
        {
            lstbEvents.Items.AddRange(eventCollection.Values.ToArray());
        }

        private void setDatagridDataSource(ElementEventInfo eventInfo)
        {
            List<EventContext> contextList = new List<EventContext>();
            contextList.AddRange(eventInfo.entityEvent);
            contextList.AddRange(eventInfo.attributeEvent);
            contextList.AddRange(eventInfo.linkEvent);

            dgvEventContext.DataSource = contextList;
            dgvEventContext.Refresh();

            if (dgvEventContext.ColumnCount == 5)
            {
                dgvEventContext.Columns[0].ReadOnly = true;
                dgvEventContext.Columns[1].ReadOnly = true;
                dgvEventContext.Columns[2].ReadOnly = true;
                dgvEventContext.Columns[4].Visible = false;
            }
        }

        private void lstbEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstbEvents.SelectedItem != null)
            {
                selectedEventInfo = lstbEvents.SelectedItem as ElementEventInfo;
                setDatagridDataSource(selectedEventInfo);
            }
        }

        private void dgvEventContext_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvEventContext.SelectedRows.Count == 1)
            {
                selectedEventContext =((EventContext)dgvEventContext.SelectedRows[0].DataBoundItem);
                txtSQLCode.Text = selectedEventContext.sqlEventMapping.GetSQLCodeMapping.ToString();
            }
        }

        private void dgvEventContext_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgvEventContext.
                Rows[e.RowIndex].Cells[e.ColumnIndex];

            if (checkCell != null)
            {
                selectedEventContext = ((EventContext)dgvEventContext.Rows[e.RowIndex].DataBoundItem);
                selectedEventContext.doSkipThisEvent = (bool)checkCell.EditedFormattedValue;
            }
        }

        private void dgvEventContext_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgvEventContext.IsCurrentCellDirty)
            {
                dgvEventContext.EndEdit();

                if (selectedEventContext.elementType == ContextElementType.Entity)
                    selectedEventInfo.entityEvent.Where(c => c.actionText.Equals(selectedEventContext.actionText) && c.timestamp.Equals(selectedEventContext.timestamp)).ToArray()[0] = selectedEventContext;
                else if (selectedEventContext.elementType == ContextElementType.Attribute)
                    selectedEventInfo.attributeEvent.Where(c => c.actionText.Equals(selectedEventContext.actionText) && c.timestamp.Equals(selectedEventContext.timestamp)).ToArray()[0] = selectedEventContext;
                else if (selectedEventContext.elementType == ContextElementType.Link)
                    selectedEventInfo.linkEvent.Where(c => c.actionText.Equals(selectedEventContext.actionText) && c.timestamp.Equals(selectedEventContext.timestamp)).ToArray()[0] = selectedEventContext;
                
                setDatagridDataSource(selectedEventInfo);
            }
        }

        private void txtSQLCode_TextChanged(object sender, EventArgs e)
        {
            if (selectedEventContext.sqlEventMapping != null && !string.Equals(txtSQLCode.Text, selectedEventContext.sqlEventMapping.GetSQLCodeMapping.ToString()))
                btnUpdate.Enabled = true;
            else
                btnUpdate.Enabled = false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!string.Equals(txtSQLCode.Text, selectedEventContext.sqlEventMapping.GetSQLCodeMapping.ToString()))
            {
                selectedEventContext.sqlEventMapping.SetSQLCodeMapping = new StringBuilder(txtSQLCode.Text);
                MessageBox.Show("Update of SQL statment mapping completed.", "Upate Generated SQL Mapping", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            btnUpdate.Enabled = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {   
            this.Hide();
        }
    }
}
